using System;
using System.Web;
using System.Web.Mvc;

namespace Caramel.Web.Mvc
{
	public static class ControllerExtensions
	{
		public static ViewResult View<T>(this Controller controller) where T : new()
		{
			return controller.View<T>(String.Empty);
		}

		public static ViewResult View<T>(this Controller controller, string viewName) where T : new()
		{
			return controller.View<T>(m => { }, viewName);
		}

		public static ViewResult View<T>(this Controller controller, Action<T> constructorAction) where T : new()
		{
			return controller.View(constructorAction, String.Empty);
		}

		public static ViewResult View<T>(this Controller controller, Action<T> constructorAction, string viewName) where T : new()
		{
			var model = Activator.CreateInstance<T>();

			constructorAction(model);
			controller.ViewData.Model = model;

			return
				new ViewResult()
				{
					ViewData = controller.ViewData,
					ViewName = viewName
				};
		}
	}
}
